package com.ham.mud.items;

import com.ham.mud.configuration.FilePaths;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by hlucas on Jun 21, 2011 at 12:59:50 PM
 */
public class ItemService {
    public static List<Item> getItems(String[] itemNames) {
        List<Item> items = new ArrayList<Item>();
        for(String itemName : itemNames) {                                      
            if(itemName == null || itemName.trim().equals("")) continue;

            try {
                Item item = loadItem(itemName);
                items.add(item);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return items;
    }
    public static Item getItem(String itemName) {
        Item item = null;
        
        try {
            item = loadItem(itemName);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }

        return item;
    }

    //name
    //shortDesc
    //longDesc
    //targeting keys
    //item type
    //eq slot

    private static Item loadItem(String fileName) throws IOException, IllegalAccessException, InstantiationException {
        File file = new File(FilePaths.getItemFilePath(fileName));
        FileInputStream inputStream = new FileInputStream(file);

        DataInputStream in = new DataInputStream(inputStream);

        String name = in.readLine();
        String shortDesc = in.readLine();
        String longDesc = in.readLine();
        String[] targetingKeys = in.readLine().split(" ");
        String itemType = in.readLine();
        String equipSlot = in.readLine();

        Class itemClass = null;
        if(itemType.equals("weapon")) {
            itemClass = Weapon.class;
        } else if(itemType.equals("decoration")){
            itemClass = Decoration.class;
        } else if(itemType.equals("junk")) {
            itemClass = Junk.class;
        } else if(itemType.equals("light")){
            itemClass = Light.class;
        } else {
            itemClass = Item.class;
        }

        Item item = (Item) itemClass.newInstance();
        item.setFileName(fileName);
        item.setName(name);
        item.setShortDescription(shortDesc);
        item.setLongDescription(longDesc);
        item.setTargetingKeys(targetingKeys);
        
        if(equipSlot != null && !equipSlot.equals("")) {
            item.setWornAt(EquipSlot.valueOf(equipSlot));
        }

        if(item instanceof Weapon) {
            Weapon weapon = (Weapon) item;
            String[] dice = in.readLine().split(" ");
            weapon.setDice(Integer.valueOf(dice[0]), Integer.valueOf(dice[1]));            
        }
        
        String price = in.readLine();
        if(price != null && !price.equals("")) {
            item.setPrice(Integer.valueOf(price));
        }
        
        in.close();
        inputStream.close();

        return item;
    }

    public static Map<EquipSlot, Item> getEquipment(String[] itemNames) {
        Map<EquipSlot, Item> eq = new HashMap<EquipSlot, Item>();
        List<Item> items = getItems(itemNames);
        for(Item item : items) {
            eq.put(item.getWornAt(), item);
        }
        return eq;
    }

    public static Item getQuestItem() {
        return getItem("quest_item");
    }
}
